package com.example.demo.service.imp;

import com.example.demo.service.TemplateService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class TemplateServiceImp implements TemplateService {


    @Override
    public String generateTemplate(String name, List<Map<String, Object>> results) {
//        企业微信markdown 信息模版
        StringBuilder sb = new StringBuilder();
        sb.append("<font color=\"red\">【" + name + "】</font>\n"); // 添加标题
        sb.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).append("\n");
        results.forEach(result -> {
//            如果有 extract 字段，还要拼接上 extract 字段
            sb.append("<font color=\"warning\">"+result.get("name")+"</font>")
                    .append(": ")
                    .append("<font color=\"red\">"+result.get("value")+"</font>")
                    .append(result.get("extract") != null ? " " + result.get("extract") : "")
                    .append("\n");
        });
        return sb.toString();
    }
}
